<?php
class LoginModel extends Model{
	public $table='admin';
	public function login(){
		$username=Q('post.username');
		$password=Q('post.password');
		$code=Q('post.code','','strtoupper');

		if(empty($username)){
			$this->error='账号不能为空';return false;
		}
		if(empty($password)){
			$this->error='密码不能为空';return false;
		}
		if($code !== session('code')){
			$this->error='验证码输入错误';return false;
		}

		$user=$this->where("username='$username'")->find();
		//判断账号是否存在
		if(!$user){
			$this->error='账号不存在';
			return false;
		}
		//验证密码是否正确
		if($user['password'] != md5($password.$user['code'])){
			$this->error='密码输入错误';
			return false;
		}
		unset($user['password']);
		unset($user['code']);
		$_SESSION['user']=$user;
		return true;
	}


}

?>